<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\helper\Hash;
use think\Db;

/**
 * @title 客户
 * @description 接口说明
 * @header name:flag require:1 default: desc:手机设备号
 */
class Client extends Api
{
    /**
     * @title 宴会与零点获取客户来源与客户经理
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/index
     * @method POST  
     * @param name:type type:int require:1 default: other: desc:1宴会2零点
     * @return users:客户经理@
     * @users id:id username:经理昵称
     * @return source:客户经理@
     * @source id:id name:名称
     * 
     * 
     */
   public function index()
   {
        $user = $this->user;
        // 客户来源
        $type = input('type');
        if(!$type){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['hid','=',$user['hid']];
         // 客户经理
        $users =  db::name('user')->where($where)->field('id,username')->select();
        $where[] = ['type','=',$type];
        $source =  db::name('source')->where($where)->field('id,name')->select();
        $data['users'] = $users;
        $data['source'] = $source;
        return $this->ajaxReturn(0,'成功',$data);
   }
    /**
     * @title 宴会预定类型
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/banquet_type
     * @method POST  
     * @return list:列表@
     * @list id:id name:名称
     * 
     */
    public function banquet_type()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $type =  db::name('banquet_types')->where($where)->field('id,name')->select();
        $data['list'] = $type;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 客户来源
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/source
     * @method POST  
     * @param name:type type:int require:1 default: other: desc:1宴会2零点
     * 
     * @return list:列表@
     * @list id:id name:名称
     * 
     */
    public function source()
    {
        $user = $this->user;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];
        $type = input('type');
        if($type == 1){
            $where[] = ['type','=',1];
            $res =  db::name('source')->where($where)->field('id,name')->select();
        }elseif($type == 2){
            $where[] = ['type','=',2];
            $res =  db::name('source')->where($where)->field('id,name')->select();
        }
        
        $data['list'] = $type;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 宴会客户
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/banquet
     * @method POST  
     * @param name:begin_time type:int require:0 default: other: desc:开始时间
     * @param name:end_time type:int require:0 default: other: desc:结束时间
     * @param name:search type:int require:0 default: other: desc:姓名或电话
     * @param name:sex type:int require:0 default: other: desc:1男2女
     * @param name:source_id type:int require:0 default: other: 客户来源
     * @param name:manager_id type:int require:0 default: other: desc:客户经理
     * 
     * 
     * @return list:列表@
     * @list id:id addtime:创建时间 name:姓名 sex:性别1男2女 mobile:手机号 source:客户来源 manager:客户经理 scheduled:订单数 money:累计消费
     */
    public function banquet()
    {
        $user = $this->user;
        $begin_time = input('begin_time');
        $end_time = input('end_time');
        $search = input('search');
        $sex = input('sex');
        $source_id = input('source_id');
        $manager_id = input('manager_id');
        $page = input('page')??1;
        if($source_id){
            $where[] = ['source_id','=',$source_id];
        }
        if($sex){
            $where[] = ['sex','=',$sex];
        }
        if($manager_id){
            $where[] = ['manager_id','=',$manager_id];
        }
        if($begin_time && $end_time){
            $begin_time = strtotime($begin_time);
            $end_time = strtotime($end_time);
            $where[] = ['addtime','between',[$begin_time,$begin_time]];
        }
        if(is_numeric($search)){
            $where[] = ['mobile','like',"%".$search."%"];

        }else{
            $where[] = ['name','like',"%".$search."%"];

        }
        $where[] = ['hid','=',$user['hid']];
        $client = db::name('client')->where($where)->field('id,name,mobile,addtime,sex,manager_id,source_id')->page("$page,10")->select();
        $total = db::name('client')->where($where)->count();
        foreach ($client as $k => $v) {
            $client[$k]['scheduled'] = db::name('scheduled')->where('uid',$v['id'])
            ->count();
            $client[$k]['money'] = db::name('check_order')->where('uid',$v['id'])
            ->sum('total_money');
            $client[$k]['manager'] = db::name('user')->where('id',$v['manager_id'])
            ->value('username');
            $client[$k]['source'] = db::name('source')->where('id',$v['source_id'])
            ->value('name');
            $client[$k]['addtime'] = date('Y-m-d H:i:s',$client[$k]['addtime']);
            unset($client[$k]['manager_id']);
            unset($client[$k]['source_id']);
        }
        $data['list'] = $client;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 宴会客户详情
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/banquet_detail
     * @method POST  
     * @param name:id type:int require:0 default: other: desc:id
     * 
     * 
     * @return list:列表@
     * @list source:客户来源 nums:预定数 num:入座数 unit:单位 position:职务 birthday:生日 address:住址 likes:喜好 diet:忌口 content:备注
     */
    public function banquet_detail()
    {
        $user = $this->user;
        $id = input('id');
       
        $where[] = ['hid','=',$user['hid']];
        
        $client = db::name('client')->where($where)->field('id,source_id,unit,position,birthday,address,likes,diet,content')->find();
        $total = db::name('client')->where($where)->count();
            
        $client['scheduled'] = db::name('scheduled')->where('uid',$client['id'])
            ->count();
            $client['money'] = db::name('check_order')->where('uid',$client['id'])
            ->sum('total_money');
            $client['source'] = db::name('source')->where('id',$client['source_id'])
            ->value('name');
            unset($client['manager_id']);
            unset($client['source_id']);
        
        $data['list'] = $client;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 零点客户
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/zero
     * @method POST  
     * @param name:begin_time type:int require:0 default: other: desc:开始时间
     * @param name:end_time type:int require:0 default: other: desc:结束时间
     * @param name:search type:int require:0 default: other: desc:姓名或电话
     * @param name:sex type:int require:0 default: other: desc:1男2女
     * @param name:source_id type:int require:0 default: other: 客户来源
     * @param name:manager_id type:int require:0 default: other: desc:客户经理
     * 
     * 
     * @return list:列表@
     * @list id:id addtime:创建时间 name:姓名 sex:性别1男2女 mobile:手机号 source:客户来源 manager:客户经理 nums:预定数 num:入座数
     */
    public function zero()
    {
        $user = $this->user;
        $begin_time = input('begin_time');
        $end_time = input('end_time');
        $search = input('search');
        $sex = input('sex');
        $source_id = input('source_id');
        $manager_id = input('manager_id');
        if($source_id){
            $where[] = ['source_id','=',$source_id];
        }
        $page = input('page')??1;
        if($sex){
            $where[] = ['sex','=',$sex];
        }
        if($manager_id){
            $where[] = ['manager_id','=',$manager_id];
        }
        if($begin_time && $end_time){
            $begin_time = strtotime($begin_time);
            $end_time = strtotime($end_time);
            $where[] = ['addtime','between',[$begin_time,$begin_time]];
        }
        if(is_numeric($search)){
            $where[] = ['mobile','like',"%".$search."%"];

        }else{
            $where[] = ['name','like',"%".$search."%"];

        }
        $where[] = ['hid','=',$user['hid']];
        $client = db::name('client_zero')->where($where)->field('id,name,mobile,addtime,sex,manager_id,source_id,nums,num')->page("$page,10")->select();
        $total = db::name('client_zero')->where($where)->count();
        foreach ($client as $k => $v) {
            $client[$k]['manager'] = db::name('user')->where('id',$v['manager_id'])
            ->value('username');
            $client[$k]['source'] = db::name('source')->where('id',$v['source_id'])
            ->value('name');
            $client[$k]['addtime'] = date('Y-m-d H:i:s',$client[$k]['addtime']);
            unset($client[$k]['manager_id']);
            unset($client[$k]['source_id']);
        }
        $data['list'] = $client;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 零点客户详情
     * @description 接口说明
     * @author 开发者
     * @url /adminApi/client/zero_del
     * @method POST  
     * @param name:id type:int require:0 default: other: desc:id
     * 
     * 
     * @return list:列表@
     * @list source:客户来源 nums:预定数 num:入座数 unit:单位 position:职务 birthday:生日 address:住址 likes:喜好 diet:忌口 content:备注
     */
    public function zero_del()
    {
        $user = $this->user;
        $id = input('id');
        $where[] = ['id','=',$id];
        $where[] = ['hid','=',$user['hid']];
        $client = db::name('client_zero')->where($where)->field('nums,num,unit,position,birthday,address,likes,diet,content,source_id')->find();
        $client['source'] = db::name('source')->where('id',$client['source_id'])->value('name');
        $data['list'] = $client;
        return $this->ajaxReturn(0,'成功',$data);
    }
}